<?php
namespace Home\Controller;

use Home\Logic\OrderLogic;
use My\Model\OrderModel;
use Utuo\OrderController;
/**
 * 代理商进货订单
 * @author song
 */
class DisShopAgentOrderController extends OrderController {
    
    /**
     * 设置订单业务类
     */
    protected function setOrderLogic() {
        OrderLogic::setDriver(OrderModel::ORDER_TYPE_AGENT_PACKAGE, array(
            'cid' => $this->cid,
            'uid' => $this->uid,
        ));
    }

    /**
     * 初始化
     */
    public function _init() {
        layout ( 'disshop' );
        $this->setModelName ( "DistributionShop" );
        $this->setModelControllerName( "DisShop" );
        $this->setOrderLogic();
    }
    
    /**
     * 获得显示确认订单模板
     */
    protected function getConfirmOrderTpl() {
        return "{$this->modelControllerName}/" ."confirmPackageOrder";
    }
    
    /**
     * 获得显示订单列表模板
     */
    protected function getOrderListTpl() {
        return "{$this->modelControllerName}/" ."agentOrderList";
    }
    
    /**
     * 展示订单
     * @return string
     */
    protected function getShowOrderTpl() {
        return "{$this->modelControllerName}/" ."showAgentOrder";
    }
    
    /**
     * 我的订单列表
     */
    public function listAction() {
        $type = I ( "type" );
        $condition = array (
            "orders.uid" => $this->uid,
        );
         
        if ($type == 1) { // 未付款
            $condition ['orders.order_status'] = OrderModel::ORDER_STATUS_PAY;
        } else if ($type == 2) { // 待签收(已付款,货到付款)
            $condition ['orders.order_status'] = array (
                "in",
                array (
                    OrderModel::ORDER_STATUS_SHIPPING,
                    OrderModel::ORDER_STATUS_SIGN
                )
            );
        } else if ($type == 3) { // 交易成功
            $condition ['orders.order_status'] = OrderModel::ORDER_STATUS_COMPLETED;
        }  else if ($type == 4) { // 退款订单
            $condition['orders.refund_times'] = array('gt', 0);
        } else { // 其它
            $condition ['orders.order_status'] = array (
                "not in",
                array (
                    OrderModel::ORDER_STATUS_PAY,
                    OrderModel::ORDER_STATUS_SHIPPING,
                    OrderModel::ORDER_STATUS_SIGN,
                    OrderModel::ORDER_STATUS_COMPLETED,
                ),
            );
        }
         
        $orderLists = OrderLogic::getList($condition);
        OrderLogic::bindOrdersDetail(&$orderLists);
        foreach ( $orderLists as &$v ) {
            $v['url'] = U(CONTROLLER_NAME.'/showOrder', "id={$v['id']}&sid={$this->cid}");
        }
        $this->assign("types", OrderModel::$TYPES);
        $this->assign ( "orderLists", $orderLists );
        $tpl = method_exists($this, "getOrderListTpl") ? $this->getOrderListTpl() : "{$this->modelControllerName}/" . ACTION_NAME;
        $this->display ( $tpl );
    }
    
}